package com.qsmx.store.app.main.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qsmx.store.app.R;
import com.qsmx.store.app.main.entity.AppInfoEntity;
import com.qsmx.store.app.main.holder.ApkListHolder;
import com.qsmx.store.core.util.StringUtil;

import java.util.List;

public class ApkListAdapter extends RecyclerView.Adapter<ApkListHolder> {
    private Context mContext;

    private List<AppInfoEntity.DataBean.VersionInfoListBean> mData;

    //定义接口
    public static interface OnItemClickListener {
        void onClick(View view, int position);

    }

    //声明自定义的监听接口
    private ApkListAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(ApkListAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public ApkListAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<AppInfoEntity.DataBean.VersionInfoListBean> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public ApkListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ApkListHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_apk_list, parent, false));
    }

    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(@NonNull ApkListHolder holder, int position) {
        AppInfoEntity.DataBean.VersionInfoListBean data = mData.get(position);
        holder.tvApkName.setText(data.getAppName());
        holder.tvClient.setText(data.getClientType());
        holder.tvVersion.setText(data.getVersionNo());
        holder.tvTips.setText(data.getUpgradeMsg());
        holder.tvTime.setText(StringUtil.getDateByTimestampNoHour(StringUtil.getStringToDateTimeNoT(data.getPublishTime())));
        holder.tvDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                monItemClickListener.onClick(view, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && !mData.isEmpty()) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
